package com.sia.springcloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.sia.springcloud.model.Point;
import com.sia.springcloud.mapper.PointMapper;
import com.sia.springcloud.service.IPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sia.springcloud.result.GridDataResult;
import com.sia.springcloud.result.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 通信协议变量表 服务实现类
 * </p>
 *
 * @author Sirius
 * @since 2021-12-27
 */
@Slf4j
@Service
public class PointService extends ServiceImpl<PointMapper, Point> implements IPointService {
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private ProtocolService protocolService;

    /**
     * 插入点位
     *
     * @param request
     * @return
     */
    @Override
    public String insertPoint(HttpServletRequest request) {
        log.info("插入点位");
        String instanceId = getInstanceIdByProtocolName(request);
        if (instanceId == null) {
            return ResultUtil.createSimpleFailureJson("协议对应的微服务未启动");
        } else {
            String priorityUrl = "http://" + instanceId + "/pointController/insertPoint";
            MultiValueMap<String, Object> params = packingParams(request);
            return restTemplate.postForObject(priorityUrl, params, String.class);
        }

    }

    /**
     * 分页查询某个设备的点位
     *
     * @param request
     * @return
     */
    @Override
    public String queryPoint(HttpServletRequest request) {
        log.info("查询点位");
        String instanceId = getInstanceIdByProtocolName(request);
        if (instanceId == null) {
            GridDataResult<Point> pointListResult = new GridDataResult<>();
            pointListResult.setTotal(0);
            pointListResult.setRoot(null);
            return JSON.toJSONString(pointListResult);
        } else {
            String priorityUrl = "http://" + instanceId + "/pointController/queryPoint";
            MultiValueMap<String, Object> params = packingParams(request);
            return restTemplate.postForObject(priorityUrl, params, String.class);
        }

    }

    /**
     * 批量删除点位
     *
     * @param request
     * @return
     */
    @Override
    public String deletePoint(HttpServletRequest request) {
        log.info("删除点位");
        String instanceId = getInstanceIdByProtocolName(request);
        if(instanceId==null){
            return ResultUtil.createSimpleFailureJson("协议对应的微服务未启动");
        }else {
            String priorityUrl = "http://" + instanceId + "/pointController/deletePoint";
            MultiValueMap<String, Object> params = packingParams(request);
            return restTemplate.postForObject(priorityUrl, params, String.class);
        }
    }

    /**
     * 更新点位
     *
     * @param request
     * @return
     */
    @Override
    public String updatePoint(HttpServletRequest request) {
        log.info("更新点位");
        String instanceId = getInstanceIdByProtocolName(request);
        if(instanceId==null){
            return ResultUtil.createSimpleFailureJson("协议对应的微服务未启动");
        }else {
            String priorityUrl = "http://" + instanceId + "/pointController/updatePoint";
            MultiValueMap<String, Object> params = packingParams(request);
            return restTemplate.postForObject(priorityUrl, params, String.class);
        }

    }


    /**
     * 获取点位表单格式
     *
     * @param request
     * @return
     */
    @Override
    public String tableStructure(HttpServletRequest request) {
        log.info("获取点位表结构");
        String instanceId = getInstanceIdByProtocolName(request);
        if(instanceId==null){
            return ResultUtil.createSimpleFailureJson("协议对应的微服务未启动");
        }else {
            String priorityUrl = "http://" + instanceId + "/pointController/tableStructure";
            MultiValueMap<String, Object> params = packingParams(request);
            return restTemplate.postForObject(priorityUrl, params, String.class);
        }
    }


    /**
     * 根据协议名获取对应的微服务实例路径
     *
     * @param request
     * @return
     */
    private String getInstanceIdByProtocolName(HttpServletRequest request) {
        return protocolService.getInstanceIdByProtocolName(request);
    }


    /**
     * 对request中的参数进行包装
     *
     * @param request
     * @return
     */
    private MultiValueMap<String, Object> packingParams(HttpServletRequest request) {
        // 复制 request 的参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
        // 附加参数值
        Set<String> keySet = parameterMap.keySet();
        for (String key : keySet) {
            String[] value = parameterMap.get(key);
            params.add(key, value[0]);
        }
        return params;
    }
}
